package com.example.lncd.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.lncd.model.User;

public class PreferenceManager {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    
    public PreferenceManager(Context context) {
        sharedPreferences = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }
    
    public void saveUser(User user) {
        editor.putLong(Constants.PREF_USER_ID, user.getId());
        editor.putString(Constants.PREF_USERNAME, user.getUsername());
        if (user.getUserType() != null) {
            editor.putString(Constants.PREF_USER_TYPE, user.getUserType().name());
        } else {
            editor.putString(Constants.PREF_USER_TYPE, "");
        }
        editor.putBoolean(Constants.PREF_IS_LOGGED_IN, true);
        editor.apply();
    }
    
    public boolean isLoggedIn() {
        return sharedPreferences.getBoolean(Constants.PREF_IS_LOGGED_IN, false);
    }
    
    public Long getUserId() {
        return sharedPreferences.getLong(Constants.PREF_USER_ID, -1);
    }
    
    public String getUsername() {
        return sharedPreferences.getString(Constants.PREF_USERNAME, null);
    }
    
    public User.UserType getUserType() {
        String userType = sharedPreferences.getString(Constants.PREF_USER_TYPE, null);
        if (userType != null && !userType.isEmpty()) {
            try {
                return User.UserType.valueOf(userType);
            } catch (IllegalArgumentException e) {
                return null;
            }
        }
        return null;
    }
    
    public void clearSession() {
        editor.clear();
        editor.apply();
    }
} 